//
//  JXCalculateBezierControllerPointer.m
//  TestAnimation
//
//  Created by xufei on 2019/4/1.
//  Copyright © 2019年 zjx. All rights reserved.
//

#import "JXCalculateBezierControllerPointer.h"
#import <CoreGraphics/CoreGraphics.h>


@implementation JXCalculateBezierControllerPointer

+ (void)getControlPointx0:(CGFloat)x0 andy0:(CGFloat)y0
                       x1:(CGFloat)x1 andy1:(CGFloat)y1
                       x2:(CGFloat)x2 andy2:(CGFloat)y2
                       x3:(CGFloat)x3 andy3:(CGFloat)y3
                     path:(UIBezierPath*) path
{
    CGFloat smooth_value =0.6;
    CGFloat ctrl1_x;
    CGFloat ctrl1_y;
    CGFloat ctrl2_x;
    CGFloat ctrl2_y;
    CGFloat xc1 = (x0 + x1) /2.0;
    CGFloat yc1 = (y0 + y1) /2.0;
    CGFloat xc2 = (x1 + x2) /2.0;
    CGFloat yc2 = (y1 + y2) /2.0;
    CGFloat xc3 = (x2 + x3) /2.0;
    CGFloat yc3 = (y2 + y3) /2.0;
    CGFloat len1 = sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
    CGFloat len2 = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
    CGFloat len3 = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
    CGFloat k1 = len1 / (len1 + len2);
    CGFloat k2 = len2 / (len2 + len3);
    CGFloat xm1 = xc1 + (xc2 - xc1) * k1;
    CGFloat ym1 = yc1 + (yc2 - yc1) * k1;
    CGFloat xm2 = xc2 + (xc3 - xc2) * k2;
    CGFloat ym2 = yc2 + (yc3 - yc2) * k2;
    ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
    ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
    ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
    ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
    
    NSLog(@"control point is: (x=%f, y=%f), (x=%f, y=%f)", ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y);
    
    [path addCurveToPoint:CGPointMake(x2, y2) controlPoint1:CGPointMake(ctrl1_x, ctrl1_y) controlPoint2:CGPointMake(ctrl2_x, ctrl2_y)];
}

+ (CAShapeLayer*)layerOfScalerWithOriginPoint: (CGPoint)origin {
    CGFloat screenW = UIScreen.mainScreen.bounds.size.width;
    CGFloat screenH = UIScreen.mainScreen.bounds.size.height;
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint: CGPointMake(0, screenH)];
    [path addLineToPoint:CGPointMake(0, 0)];
    [path moveToPoint:CGPointMake(0, screenH)];
    [path addLineToPoint:CGPointMake(screenW, screenH)];
    
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.path = path.CGPath;
    layer.lineWidth = 10;
    layer.strokeColor = UIColor.greenColor.CGColor;
    layer.backgroundColor = UIColor.clearColor.CGColor;
    return layer;
}

+ (CAShapeLayer*)layerOfPoints: (CGPoint*)points count:(NSUInteger)count {
    CGMutablePathRef path = CGPathCreateMutable();
    
    
    int i = 0;
    const int rectW = 2;
    while (i < count) {
        CGPoint point = points[i];
//        CGPoint rOrigin = CGPointMake(point.x - 1, point.y - 1);
        CGRect pointRect = CGRectMake(point.x - 1, point.y - 1, rectW, rectW);
        CGPathAddRect(path, NULL, pointRect);
        
        i++;
    }
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.path = path;
    CFRelease(path);
    layer.lineWidth = 10;
    layer.strokeColor = UIColor.greenColor.CGColor;
    layer.backgroundColor = UIColor.clearColor.CGColor;
    
    return layer;
    
}

@end



